<template>
	<view class="container">
		<titlebar mode="" :back="true" text="通知"></titlebar>
		<view class="message-kinds" :style="'padding-top:'+navigationBarHeight+'px ;'">
			<!-- 系统通知 -->
			<!-- <view class="row">
				<view class="left">
					<image class="avatar" src="@appstatic/message/message_icon_systemmessage.png" mode=""></image>
					<text class="all-message">系统通知</text>
				</view>
				<view class="right">
					<text class="right-text">{{systemChecked?'开启':'关闭'}}</text>
					<switch :checked="systemChecked?true:false" @change="systemChange" color="#EA5404" style="transform:scale(0.7)"/>
				</view>
			</view> -->
			<!-- 互动通知 -->
			<view class="row">
				<view class="left">
					<image class="avatar" src="@appstatic/message/message_icon_interactmessage.png" mode=""></image>
					<text class="all-message">互动通知</text>
				</view>
				<view class="right">
					<text class="right-text">{{interactChecked?'开启':'关闭'}}</text>
					<switch :checked="interactChecked?true:false" @change="interactChange" color="#EA5404" style="transform:scale(0.7)"/>
				</view>
			</view>
			<!-- 订单通知 -->
			<view class="row">
				<view class="left">
					<image class="avatar" src="@appstatic/message/message_icon_ordermessage.png" mode=""></image>
					<text class="all-message">订单通知</text>
				</view>
				<view class="right">
					<text class="right-text">{{orderChecked?'开启':'关闭'}}</text>
					<switch :checked="orderChecked?true:false" @change="orderChange" color="#EA5404" style="transform:scale(0.7)"/>
				</view>
			</view>
			<!-- 告警 -->
			<view class="row">
				<view class="left">
					<image class="avatar" src="@appstatic/message/message_icon_systemmessage.png" mode=""></image>
					<text class="all-message">车辆告警</text>
				</view>
				<view class="right">
					<text class="right-text">{{warnChecked?'开启':'关闭'}}</text>
					<switch :checked="warnChecked?true:false" @change="warnChange" color="#EA5404" style="transform:scale(0.7)"/>
				</view>
			</view>
			<!-- 商城 -->
			<view class="row">
				<view class="left">
					<image class="avatar" src="./static/avatar_mallmessage.png" mode=""></image>
					<text class="all-message">商城通知</text>
				</view>
				<view class="right">
					<text class="right-text">{{mallChecked?'开启':'关闭'}}</text>
					<switch :checked="mallChecked?true:false" @change="mallChange" color="#EA5404" style="transform:scale(0.7)"/>
				</view>
			</view>
			<!-- 活动 -->
			<view class="row">
				<view class="left">
					<image class="avatar" src="./static/avatar_activitymessage.png" mode=""></image>
					<text class="all-message">活动通知</text>
				</view>
				<view class="right">
					<text class="right-text">{{activityChecked?'开启':'关闭'}}</text>
					<switch :checked="activityChecked?true:false" @change="activityChange" color="#EA5404" style="transform:scale(0.7)"/>
				</view>
			</view>
		</view>
		

	</view>
</template>

<script>
	import titlebar from '@/components/titlebar/titlebar'
	const app = getApp();
	const util = require("@utils/util.js");
	const network = require('../../manager/network-manager.js');
	const dialog=require("../../utils/dialog.js");
	export default {
		components: {
			titlebar
		},
		data() {
			return {
				
				text:"通知设置",
				img:"../../static/message/message_icon_titlebar.png",
				navigationBarHeight:app.globalData.navigationBarHeight,
				setting:[],
				systemChecked:true,
				orderChecked:true,
				interactChecked:true,
				warnChecked:true,
				mallChecked:true,
				activityChecked:true,
			}
		},
		onLoad: function(options) {
		   // this.loadData(true);
		   if (util.isLogin(getApp())) {
			   this.$fire.$on('message', function(){
			   				this.refresh();
			   });
		   }
		   
		},
		onShow: function(options) {
			console.log("onShow");
			if (util.isLogin(getApp())) {
				this.refresh();
			}else{
				
			}
		},
		
		methods: {
			refresh:function(){
				this.getSetting();
			},
			
			//根据用户查询设置
			async getSetting(){
				var that=this;
				var uri=app.globalData.config.interfaces.URL_GET_USSERSETTINGMESSAGELIST;
				var params={
					
				};
				network.get(uri,params,'form').then(res=>{
					if(res.code==200){
						 that.setting=res.data;
						 for(var i=0;i<that.setting.length;i++){
							 if(that.setting[i].messageType==1){
								 that.orderChecked=false
							 }
							 if(that.setting[i].messageType==2){
								 that.interactChecked=false
							 }
							 if(that.setting[i].messageType==4){
							 	that.systemChecked=false
							 }
							 if(that.setting[i].messageType==5){
							 	that.warnChecked=false
							 }
							 if(that.setting[i].messageType==6){
							 	that.mallChecked=false
							 }
							 if(that.setting[i].messageType==8){
							 	that.activityChecked=false
							 }
						 }
					}else{
						uni.showToast({
							title:res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch()
			},
			systemChange(){
				var that=this;
				
				if(!that.systemChecked){//未选中状态,点击删除
					var id=that.setting.find(x=>x.messageType==4).id
					var uri=app.globalData.config.interfaces.URL_GET_DELETEUSERSETTING;
					var params={
						id:id
					};
					network.post(uri,params,'form').then(res=>{
						if(res.code==200){
							 that.systemChecked=true;
							 that.refresh()
						}else{
							uni.showToast({
								title:res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch()
				}else{//选中状态,选中添加
					var uri=app.globalData.config.interfaces.URL_GET_INSERTUSERSETTING;
					var params={
						messageType:4
					};
					network.post(uri,params,'json').then(res=>{
						if(res.code==200){
							 that.systemChecked=false;
							 that.refresh()
						}else{
							uni.showToast({
								title:res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch()
				}
			},
			interactChange(){
				var that=this;
				
				if(!that.interactChecked){//未选中状态,点击删除
					var id=that.setting.find(x=>x.messageType==2).id
					var uri=app.globalData.config.interfaces.URL_GET_DELETEUSERSETTING;
					var params={
						id:id
					};
					network.post(uri,params,'form').then(res=>{
						if(res.code==200){
							 that.interactChecked=true;
							 that.refresh()
						}else{
							uni.showToast({
								title:res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch()
				}else{//选中状态,选中添加
					var uri=app.globalData.config.interfaces.URL_GET_INSERTUSERSETTING;
					var params={
						messageType:2
					};
					network.post(uri,params,'json').then(res=>{
						if(res.code==200){
							 that.interactChecked=false;
							 that.refresh()
						}else{
							uni.showToast({
								title:res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch()
				}
			},
			orderChange(){
				var that=this;
				if(!that.orderChecked){//未选中状态,点击删除
					var id=that.setting.find(x=>x.messageType==1).id
					var uri=app.globalData.config.interfaces.URL_GET_DELETEUSERSETTING;
					var params={
						id:id
					};
					network.post(uri,params,'form').then(res=>{
						if(res.code==200){
							 that.orderChecked=true;
							 that.refresh()
						}else{
							uni.showToast({
								title:res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch()
				}else{//未选中状态,选中添加
					var uri=app.globalData.config.interfaces.URL_GET_INSERTUSERSETTING;
					var params={
						messageType:1
					};
					network.post(uri,params,'json').then(res=>{
						if(res.code==200){
							 that.orderChecked=false;
							 that.refresh()
						}else{
							uni.showToast({
								title:res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch()
				}
			},
			//车辆告警
			warnChange(){
				var that=this;
				if(!that.warnChecked){//未选中状态,点击删除
					var id=that.setting.find(x=>x.messageType==5).id
					var uri=app.globalData.config.interfaces.URL_GET_DELETEUSERSETTING;
					var params={
						id:id
					};
					network.post(uri,params,'form').then(res=>{
						if(res.code==200){
							 that.warnChecked=true;
							 that.refresh()
						}else{
							uni.showToast({
								title:res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch()
				}else{//未选中状态,选中添加
					var uri=app.globalData.config.interfaces.URL_GET_INSERTUSERSETTING;
					var params={
						messageType:5
					};
					network.post(uri,params,'json').then(res=>{
						if(res.code==200){
							 that.warnChecked=false;
							 that.refresh()
						}else{
							uni.showToast({
								title:res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch()
				}
			},
			//商城
			mallChange(){
				var that=this;
				if(!that.mallChecked){//未选中状态,点击删除
					var id=that.setting.find(x=>x.messageType==6).id
					var uri=app.globalData.config.interfaces.URL_GET_DELETEUSERSETTING;
					var params={
						id:id
					};
					network.post(uri,params,'form').then(res=>{
						if(res.code==200){
							 that.mallChecked=true;
							 that.refresh()
						}else{
							uni.showToast({
								title:res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch()
				}else{//未选中状态,选中添加
					var uri=app.globalData.config.interfaces.URL_GET_INSERTUSERSETTING;
					var params={
						messageType:6
					};
					network.post(uri,params,'json').then(res=>{
						if(res.code==200){
							 that.mallChecked=false;
							 that.refresh()
						}else{
							uni.showToast({
								title:res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch()
				}
			},
			//活动
			activityChange(){
				var that=this;
				if(!that.activityChecked){//未选中状态,点击删除
					var id=that.setting.find(x=>x.messageType==8).id
					var uri=app.globalData.config.interfaces.URL_GET_DELETEUSERSETTING;
					var params={
						id:id
					};
					network.post(uri,params,'form').then(res=>{
						if(res.code==200){
							 that.activityChecked=true;
							 that.refresh()
						}else{
							uni.showToast({
								title:res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch()
				}else{//未选中状态,选中添加
					var uri=app.globalData.config.interfaces.URL_GET_INSERTUSERSETTING;
					var params={
						messageType:8
					};
					network.post(uri,params,'json').then(res=>{
						if(res.code==200){
							 that.activityChecked=false;
							 that.refresh()
						}else{
							uni.showToast({
								title:res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch()
				}
			}
		}
	}
</script>
<style>
	page {
		background: url('@appstatic/img/my/bg-img.jpg') no-repeat center center fixed;
		background-size: cover;
		box-sizing: border-box;
	}
</style>
<style lang="less">
.container {
	
}
.message-kinds {
	display: flex;
	flex-direction: column;
	padding-left: 16px;
	padding-right: 27px;
}
.row {
	height: 83px;
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	vertical-align: middle;
	align-items: center;
}
.left {
	
	display: flex;
	vertical-align: middle;
	align-items: center;
	flex-direction: row;
}
.uni-bage-left-margin {
	margin-left: 10px;
}
.avatar {
	width: 42px;
	height: 42px;
}
.text {
	/* padding-left: 12px; */
	width: 150px;
}
.all-message {
	line-height: 22.4px;
	font-size: 16px;
	color: #3D3A3A;
	padding-left: 8px;
}
.new-message {
	line-height: 16.8px;
	font-size: 12px;
	color: #ADB0AE;
	width:100%;
	min-width: 0;
	
	
	overflow-x: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	-webkit-line-clamp: 1;
	-webkit-box-orient: vertical;
}
.ellipsis {
	
}
.right-text {
	font-weight: 400;
	font-size: 14px;
	line-height: 20px;
	color: #ADB0AE;
}

</style>
